/*
 * @Author: NtyAaron
 * @Date: 2021-04-12 11:13:45
 * @LastEditors: NtyAaron
 * @LastEditTime: 2021-06-23 14:26:07
 * @Description: file content
 * @FilePath: \dwq\pages\check\checkShip\checkShip.js
 */
const app = getApp()
import { getRailwayList } from '../../../api/checkRailway/checkRailway'
import { _t } from '../../../i18n/base'
import { getPosterList } from '../../../api/advert/advert'

Page({
  data: {
    index: true,
    cityShow: false,
    entryExitShow: false,
    terminalShow: false,
    citys: '',
    entryExit: '',
    terminal: '',
    language: '',
    tips: '',
    hkStatus: false,
    terminalActions: [],
    inAndOut: [],
    langStatus: '',
    objReq: {
      arrdep: '',
      terminal: '',
      lang: 'sc',
      local: '',
      destination: '',
    },
    domesticShippingSchedule: [],
    getAdvertObj: {
      categoryType: '',
      language: '',
      posterName: '',
      posterType: 8,
      showType: 1,
      trafficType: 3,
    },
    bannerList: [],
  },
  // 境内
  domestic() {
    this.setData({
      index: true,
    })
  },
  // 境外
  abroad() {
    this.setData({
      index: false,
    })
  },
  // 查询
  check() {
    if (this.data.city == '' || this.data.entryExit == '' || this.data.terminal == '') {
      wx.showToast({
        title: this.data.lnag['请完善信息'],
        icon: 'none',
      })
    } else {
      let req = JSON.stringify(this.data.objReq)
      wx.navigateTo({
        url: `/pages/check/shipResult/shipResult?req=${req}`,
      })
    }
  },
  // 初始化语言
  loadData() {
    let _lang = _t()
    let data = {
      cityActions: [
        {
          name: _lang['香港'],
        },
        {
          name: _lang['澳门'],
        },
      ],
      entryExitActions: [
        {
          name: _lang['离港'],
          key: 1,
        },
        {
          name: _lang['入港'],
          key: 2,
        },
      ],
      entryDepartureActions: [
        {
          name: _lang['进站'],
          key: 1,
        },
        {
          name: _lang['离站'],
          key: 2,
        },
      ],
      hkActions: [
        {
          name: _lang['全部站点'],
          key: 0,
        },
        {
          name: _lang['中国客运码头'],
          key: 1,
        },
        {
          name: _lang['港澳码头'],
          key: 2,
        },
        // {
        //   name: _lang['屯门客运码头'],
        //   key: 3,
        // },
      ],
      macActions: [
        {
          name: _lang['澳门外港'],
          key: 'pe',
        },
        {
          name: _lang['氹仔'],
          key: 'tp',
        },
        {
          name: _lang['澳门内港'],
          key: 'pi',
        },
      ],
    }
    this.setData(data)
  },
  // 打开城市选择弹窗
  cityShowHandle() {
    this.setData({
      cityShow: true,
    })
  },
  // 打开出入境选择弹窗
  entryExitShowHandle() {
    this.setData({
      entryExitShow: true,
    })
  },
  // 打开终点选择弹窗
  terminalShowHandle() {
    if (this.data.city == '') {
      wx.showToast({
        title: this.data.lang['请先选择所在城市'],
        icon: 'none',
      })
    } else {
      this.setData({
        terminalShow: true,
      })
    }
  },
  // 关闭城市选择弹窗
  closeCityShowHandle() {
    this.setData({
      cityShow: false,
    })
  },
  // 关闭出入境选择弹窗
  closeentryExitShowHandle() {
    this.setData({
      entryExitShow: false,
    })
  },
  // 关闭终点选择弹窗
  closeterminalShowHandle() {
    this.setData({
      terminalShow: false,
    })
  },
  // 城市选择
  citySelectHandle({ detail }) {
    this.setData({
      city: detail.name,
      cityShow: false,
      entryExit: '',
      terminal: '',
      ['objReq.terminal']: '',
      ['objReq.destination']: '',
      terminalActions: detail.name == this.data.lang['香港'] ? this.data.hkActions : this.data.macActions,
      tips: detail.name == this.data.lang['香港'] ? this.data.lang['类型选择'] : this.data.lang['码头选择'],
      hkStatus: detail.name == this.data.lang['香港'] ? false : true,
      inAndOut: detail.name == this.data.lang['香港'] ? this.data.entryDepartureActions : this.data.entryExitActions,
    })
    this.setLanguage()
  },
  // 出入境选择
  entryExitSelectHandle({ detail }) {
    this.setData({
      entryExit: detail.name,
      entryExitShow: false,
      ['objReq.arrdep']: detail.key,
    })
  },
  // 终点选择
  terminalActionsSelectHandle({ detail }) {
    this.setData({
      terminal: detail.name,
      ['objReq.local']: detail.key,
      ['objReq.terminal']: detail.key,
      ['objReq.destination']: detail.name,
      terminalShow: false,
    })
  },
  // 获取广告
  async getPosterList() {
    this.setData({
      'getAdvertObj.language': this.data.langStatus,
    })
    let { result } = await getPosterList(this.data.getAdvertObj)
    if (result) {
      result.map((item) => {
        item.bannerLink = `${app.globalData.host}/attach/showPic?attachId=${item.pic}`
      })
      this.setData({
        bannerList: result,
      })
    }
  },
  // 点击广告
  adClickHandle(e) {
    let link = e.currentTarget.dataset.link
    wx.setClipboardData({
      data: link,
      success: () => {
        wx.showToast({
          title: this.data.lang['已复制网站网址'],
          icon: 'none',
        })
      },
    })
  },
  // 复制
  copyLinkHandle({ currentTarget }) {
    let type = currentTarget.dataset.type
    //九州港
    let JZUrl = {
      zh: 'http://www.zhjzg.com/',
      tw: 'http://www.zhjzg.com/',
      en: 'http://www.zhjzg.com/',
    }
    //中山港
    let ZSUrl = {
      zh: 'https://www.zspassenger.com.cn/zh-CN',
      tw: 'https://www.zspassenger.com.cn/zh-CN',
      en: 'https://www.zspassenger.com.cn/zh-CN ',
    }
    //香港海事
    let XGUrl = {
      zh: 'https://crossboundaryferryservices.mardep.gov.hk/sc/',
      tw: 'https://crossboundaryferryservices.mardep.gov.hk/tc/',
      en: 'https://crossboundaryferryservices.mardep.gov.hk/en/',
    }
    //澳门海事
    let AMUrl = {
      zh: 'https://m.marine.gov.mo/seawayScheduled/RealTimeSailing.aspx?type=dataArrivalXML&local=PE&lang=SC',
      tw: 'https://m.marine.gov.mo/seawayScheduled/RealTimeSailing.aspx?type=dataArrivalXML&local=PE',
      en: 'https://m.marine.gov.mo/seawayScheduled/RealTimeSailing.aspx?type=dataArrivalXML&local=PE&lang=ENG',
    }
    let language = wx.getStorageSync('language')
    let url = ''
    if (type == 1) {
      url = JZUrl[language]
    } else if (type == 2) {
      url = ZSUrl[language]
    } else if (type == 3) {
      url = XGUrl[language]
    } else {
      url = AMUrl[language]
    }
    if (url == undefined) {
      wx.showToast({
        title: this.data.lang['当前没有该网址'],
        icon: 'none',
      })
    }
    wx.setClipboardData({
      data: url,
      success: () => {
        wx.showToast({
          title: this.data.lang['已复制网站网址'],
          icon: 'none',
        })
      },
    })
  },
  // 路线规划
  routerJump() {
    wx.navigateTo({
      url: '/pages/interactionMap/interactionMap',
    })
  },
  // 设定接口参数语言
  setLanguage() {
    let language = wx.getStorageSync('language')
    if (this.data.city == this.data.lang['香港']) {
      if (language == 'zh') {
        this.setData({
          ['objReq.lang']: 'sc',
        })
      }
      if (language == 'tw') {
        this.setData({
          ['objReq.lang']: 'tc',
        })
      }
      if (language == 'en') {
        this.setData({
          ['objReq.lang']: 'en',
        })
      }
    } else {
      if (language == 'zh') {
        this.setData({
          ['objReq.lang']: 'sc',
        })
      }
      if (language == 'tw') {
        this.setData({
          ['objReq.lang']: 'chn ',
        })
      }
      if (language == 'en') {
        this.setData({
          ['objReq.lang']: 'eng ',
        })
      }
    }
  },
  // 获取船期境内数据
  async getRailwayList(e) {
    let { result } = await getRailwayList({ language: e, type: 2 })
    this.setData({
      domesticShippingSchedule: result,
    })
  },
  onLoad() {
    let lang = wx.getStorageSync('lang')
    this.getRailwayList(lang)
    let language = wx.getStorageSync('language')
    this.setData({
      language,
      langStatus: lang,
      lang: _t(),
    })
    this.getPosterList()
    this.setData({
      tips: this.data.lang['站点选择'],
    })
    wx.setNavigationBarTitle({
      title: this.data.lang['船期查询'],
    })
    this.loadData()
  },
})
